package org.qsoft.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipUtils {

	static final int BUFFER = 8192;

	public static void zip(String destFile, File[] files, String[] names) {
		try {
			BufferedInputStream origin = null;
			FileOutputStream dest = new FileOutputStream(destFile);
			ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
					dest));
			byte data[] = new byte[BUFFER];

			for (int i = 0; i < files.length; i++) {
				if (files[i].exists()) {
					FileInputStream fi = new FileInputStream(files[i]);
					origin = new BufferedInputStream(fi, BUFFER);
					ZipEntry entry = new ZipEntry(names == null ? files[i]
							.getName() : names[i]);
					out.putNextEntry(entry);
					int count;
					while ((count = origin.read(data, 0, BUFFER)) != -1) {
						out.write(data, 0, count);
					}
					origin.close();
				}
			}
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
